package ytl;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLException;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.TableCellRenderer;

public class Systems {

    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    JLabel logo = new JLabel();
    String str = new String();
    JLabel Error_logo = new JLabel();
    JFrame Error_Frame = new JFrame("Error !!");
    
    Container Error_contentPane = Error_Frame.getContentPane();
    Point pt = new Point();
    Box systems_menu_box = Box.createVerticalBox();
    Box systems_data_box = Box.createVerticalBox();
    
    JScrollPane Inventory_scrollpane = new JScrollPane();
    boolean sound = true;
    
    JTable Inventory_table = new  JTable(Inventory.Inventory_disc_model){
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
            return false;
        }
        @Override
        public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
            Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
            
                if(Index_row == 0){
                    comp.setBackground(Color.black);
                    comp.setForeground(Color.white);
                }
                else if (Index_row % 2 == 0 && !isCellSelected(Index_row, Index_col)) {
                    comp.setBackground(Color.gray);
                    comp.setForeground(Color.white);
                }
                else if (Index_row % 2 == 1 && !isCellSelected(Index_row, Index_col)) {
                    comp.setBackground(Color.white);
                    comp.setForeground(Color.black);
                } 
                else {
                    comp.setBackground(Color.red);
                    comp.setForeground(Color.white);
                }
            return comp;
        }
    };
    
    JTextArea Information = new JTextArea();
    Box Info_box = Box.createVerticalBox();
    Box systems_logo_box = Box.createVerticalBox();
    
    Box Disc_List = Box.createHorizontalBox();
    Box Fields = Box.createHorizontalBox();
    Box Field1 = Box.createVerticalBox();
    Box Field2 = Box.createVerticalBox();
    Box Field3 = Box.createVerticalBox();
    Box Field4 = Box.createVerticalBox();
    Box Buttons = Box.createVerticalBox();
    
    
    JLabel Discontinued_List = new JLabel("List of discontinued products                       ");
    
    JLabel url = new JLabel("     String URL:");
    JLabel db = new JLabel("     Database Name:");
    JLabel driver = new JLabel("     Database Driver:");
    JLabel username = new JLabel("            Username: ");
    JLabel password = new JLabel("            Password: ");
    JLabel tax = new JLabel("            Tax: ");
    
    JTextField url_txt = new JTextField("jdbc:mysql://localhost:3306/");
    JTextField db_txt = new JTextField("[ytl]");
    JTextField driver_txt = new JTextField("com.mysql.jdbc.Driver");
    JTextField username_txt = new JTextField("root");
    JTextField password_txt = new JTextField("");
    JTextField tax_txt = new JTextField("0.12");
    Dimension W = new Dimension();
    Dimension X = new Dimension();
    Dimension Y = new Dimension();
    Dimension Z = new Dimension();
    JButton Continue = new JButton("Continue Product");
    JButton Change = new JButton("Change");
    JButton Sound = new JButton("Sound Off");
    
    // </editor-fold>
    
    public void Init_Systems(){
        
        Discontinued_List.setFont(new java.awt.Font("Verdana", 0, 16));
        Disc_List.add(Discontinued_List);
        Disc_List.add(Continue);
        Disc_List.setBorder(BorderFactory.createEmptyBorder(0,0,0,50));
        W.setSize(100,60);
        X.setSize(170,20);
        Y.setSize(110,20);
        Z.setSize(70,20);
        Change.setMaximumSize(W);
        Change.setMinimumSize(W);
        
        Continue.addActionListener(new Action());
        Main_Menu.Button_Options2(Continue);
        
        Sound.addActionListener(new Action());
        Main_Menu.Button_Options2(Sound);
        
        Change.addActionListener(new Action());
        Main_Menu.Button_Options2(Change);
        
        url_txt.setMinimumSize(X);
        db_txt.setMinimumSize(X);
        driver_txt.setMinimumSize(X);
        username_txt.setMinimumSize(Z);
        password_txt.setMinimumSize(Z);
        tax_txt.setMinimumSize(Z);
        
        url_txt.setMaximumSize(X);
        db_txt.setMaximumSize(X);
        driver_txt.setMaximumSize(X);
        username_txt.setMaximumSize(Z);
        password_txt.setMaximumSize(Z);
        tax_txt.setMaximumSize(Z);
        
        Field1.add(url_txt);
        Field1.add(db_txt);
        Field1.add(driver_txt);
        Field2.add(username_txt);
        Field2.add(password_txt);
        Field2.add(tax_txt);
        
        url.setMinimumSize(Y);
        db.setMinimumSize(Y);
        driver.setMinimumSize(Y);
        username.setMinimumSize(Y);
        password.setMinimumSize(Y);
        tax.setMinimumSize(Y);
        
        url.setMaximumSize(Y);
        db.setMaximumSize(Y);
        driver.setMaximumSize(Y);
        username.setMaximumSize(Y);
        password.setMaximumSize(Y);
        tax.setMaximumSize(Y);
        
        Field3.add(url);
        Field3.add(db);
        Field3.add(driver);
        Field4.add(username);
        Field4.add(password);
        Field4.add(tax);
        
        Fields.add(Change);
        Fields.add(Field3);
        Fields.add(Field1);
        Fields.add(Field4);
        Fields.add(Field2);
        
        
        Information.setText("");
        logo.setIcon(new javax.swing.ImageIcon("pictures/YTL Logo.png"));
        Inventory_table.setRowHeight(24);
        Inventory_scrollpane.setViewportView(Inventory_table);
        Information.setEditable(false);
        Information.setFont(new java.awt.Font("Verdana", 0, 12));
        Information.setBackground(Color.black);
        Information.setForeground(Color.white);
        
        Information.append(" Welcome to YTL !!\n");
        Information.append("\n This project was made by: \n");
        Information.append(" Yali Tan Lim \n");
        Information.append("  - yali_lim@yahoo.com \n\n");
        Information.append(" Martin Spencer Tay Sy \n");
        Information.append("  - martin_spencer_sy@yahoo.com \n\n");
        Information.append(" Fritz Kevin Salvador Flores \n");
        Information.append("  - yanagi_apocalypse@yahoo.com \n");
        
        Error_Frame.setSize(new Dimension(280, 130));
        Error_Frame.setResizable(false);
        Error_contentPane.setBackground(Color.white);
        Error_logo.setIcon(new javax.swing.ImageIcon("pictures/error/error_general.png"));       
        Error_contentPane.add(Error_logo,BorderLayout.BEFORE_LINE_BEGINS);
    }

    protected JComponent Systems_Menu() {
        systems_menu_box.removeAll();
        Info_box.removeAll();
        
        Info_box.setBorder(BorderFactory.createEmptyBorder(10,250,40,10));
        systems_logo_box.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        systems_menu_box.setBorder(BorderFactory.createEmptyBorder(0,-241,0,0));
        systems_menu_box.removeAll();
        systems_logo_box.add(logo);
        systems_menu_box.add(systems_logo_box);
        Info_box.add(Information);
        systems_menu_box.add(Info_box);
        systems_menu_box.add(Sound);
 
        return systems_menu_box;
    }
    
    protected JComponent Systems_Data() {
        systems_data_box.removeAll();
        
        systems_data_box.add(Box.createVerticalStrut(5));
        systems_data_box.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        systems_data_box.add(Disc_List);
        systems_data_box.add(Box.createVerticalStrut(5));
        systems_data_box.add(Inventory_scrollpane);
        systems_data_box.add(Box.createVerticalStrut(20));
        systems_data_box.add(Fields);
        
        return systems_data_box;
    }

    public class Action implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent ae){
            str = ae.getActionCommand();
            try{
                if(str.equals("Change")){
                    Main_Menu.DBConnection.Init_DBC(url_txt.getText(), driver_txt.getText(), db_txt.getText(), username_txt.getText(), password_txt.getText(), tax_txt.getText());
                    
                    File file = new File("systems.txt");

                    if (!file.exists())file.delete();
                    try {
                        BufferedWriter output = new BufferedWriter(new FileWriter(file));
                        output.write("[YTL] General Merchandise Systems Data"); output.newLine();
                        output.write(url_txt.getText()); output.newLine();
                        output.write(driver_txt.getText()); output.newLine();
                        output.write(db_txt.getText()); output.newLine();
                        output.write(username_txt.getText()); output.newLine();
                        output.write(password_txt.getText()); output.newLine();
                        output.write(tax_txt.getText()); output.newLine();

                        output.close();
                    } catch (IOException ex) {Error();}
                }
                else if(str.equals("Continue Product")){
                    try{Main_Menu.DBConnection.Query_Send("UPDATE product SET Status = 'Available' WHERE product.Name ='"+ Inventory.Inventory_disc_model.getValueAt(Inventory_table.getSelectedRow(), 0).toString() +"' AND product.Category ='"+ Inventory.Inventory_disc_model.getValueAt(Inventory_table.getSelectedRow(), 1).toString() +"' AND product.Size ='"+ Inventory.Inventory_disc_model.getValueAt(Inventory_table.getSelectedRow(), 2).toString() +"' AND product.Color ='"+ Inventory.Inventory_disc_model.getValueAt(Inventory_table.getSelectedRow(), 3).toString() +"'AND product.PurchasePrice ='"+ Inventory.Inventory_disc_model.getValueAt(Inventory_table.getSelectedRow(), 4).toString().substring(4) +"' AND product.Picture ='"+ Inventory.Inventory_disc_model.getValueAt(Inventory_table.getSelectedRow(), 5).toString() +"'AND product.Quantity ='"+ Inventory.Inventory_disc_model.getValueAt(Inventory_table.getSelectedRow(), 6).toString() +"'AND product.SellingPrice ='"+ Inventory.Inventory_disc_model.getValueAt(Inventory_table.getSelectedRow(), 7).toString().substring(4) +"';");
                        
                        Main_Menu.DBConnection.DBClose();
                        Inventory.Inventory_model.insertRow(1,new Object[]{Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 0), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 1), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 2), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 3), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 4), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 5),  Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 6) , Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 7)});
                        Inventory.Inventory_disc_model.removeRow(Inventory_table.getSelectedRow());
                    } catch (ClassNotFoundException ex) {} catch (SQLException ex) {Error();} catch (NullPointerException ex) {Error();} catch(ArrayIndexOutOfBoundsException x){Error();} 
                    Inventory_table.repaint();
                }
                else if(str.equals("Sound Off")){
                    sound = false;
                    Sound.setText("Sound On");
                }
                else if(str.equals("Sound On")){
                    sound = true;
                    Sound.setText("Sound Off");
                }
            }catch (NullPointerException e){
                Error();
            }
        }
    }
    
    public static void Systems() {
           
    }
    
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Systems();
            }
        });
    }
    
    public void Error(){
        pt = Main_Menu.frame.getLocation();
        Error_Frame.setLocation(pt.x + 280,pt.y + 140);
        try{
            Error_thread threads = new Error_thread();
            threads.start();
        }catch(IllegalThreadStateException e){
        }
    }
    
    class Error_thread extends Thread {
        
        @Override
        public void run() {
            try {
                Error_contentPane.removeAll();
                Error_contentPane.add(Error_logo,BorderLayout.BEFORE_LINE_BEGINS);
                Error_Frame.setVisible(true);
                if(sound==true)
                try {
                AudioClip no = Applet.newAudioClip(new URL("file:pictures/no.wav"));
                no.play();
                } catch (MalformedURLException ex) {
                }
                Thread.sleep(1000);
                Error_Frame.setVisible(false);
            } catch (InterruptedException e) {
    }}}
}